package org.diffshop.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.diffshop.common.utils.PageUtils;
import org.diffshop.common.utils.Query;

import org.diffshop.ware.dao.WmsPurchaseDetailDao;
import org.diffshop.ware.entity.WmsPurchaseDetailEntity;
import org.diffshop.ware.service.WmsPurchaseDetailService;


@Service("wmsPurchaseDetailService")
public class WmsPurchaseDetailServiceImpl extends ServiceImpl<WmsPurchaseDetailDao, WmsPurchaseDetailEntity> implements WmsPurchaseDetailService {

    /**
     * 分页查询带条件
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<WmsPurchaseDetailEntity> queryWrapper = new LambdaQueryWrapper<>();

        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)){
            queryWrapper.and(wrapper -> {
                wrapper.eq(WmsPurchaseDetailEntity::getPurchaseId, key)
                        .or().eq(WmsPurchaseDetailEntity::getSkuId, key);
            });
        }
        String status = (String) params.get("status");
        if (StringUtils.isNotEmpty(status)){
            queryWrapper.eq(WmsPurchaseDetailEntity::getStatus, status);
        }
        String wareId = (String) params.get("wareId");
        if (StringUtils.isNotEmpty(wareId)){
            queryWrapper.eq(WmsPurchaseDetailEntity::getWareId, wareId);
        }
        IPage<WmsPurchaseDetailEntity> page = this.page(
                new Query<WmsPurchaseDetailEntity>().getPage(params), queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据采购单id查询列表
     * @param purchaseId
     * @return
     */
    @Override
    public List<WmsPurchaseDetailEntity> listByPurchaseId(Long purchaseId) {
        LambdaQueryWrapper<WmsPurchaseDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmsPurchaseDetailEntity::getPurchaseId, purchaseId);

        return this.list(queryWrapper);
    }

}
